package com.zros.modules.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zros.modules.business.dto.CreateTeacherDto;
import com.zros.modules.business.dto.QueryTeacherDto;
import com.zros.modules.business.dto.TeaCourseDto;
import com.zros.modules.business.dto.UpdateTeacherDto;
import com.zros.modules.business.entity.OrgTea;
import com.zros.modules.business.entity.TeaCour;
import com.zros.modules.business.entity.Teacher;
import com.zros.modules.business.mapper.TeacherMapper;
import com.zros.modules.business.service.IOrgTeaService;
import com.zros.modules.business.service.ITeaCourService;
import com.zros.modules.business.service.ITeacherService;
import com.zros.modules.business.vo.TeacherInfoVo;

/**
 * <p>
 * 教师表 服务实现类
 * </p>
 *
 * @author pangyinqiang
 * @since 2019-04-16
 */
@Service
@Slf4j
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {

	@Autowired
	IOrgTeaService orgTeaService;
	
	@Autowired
	ITeaCourService teaCourService;
	
	@Autowired
	TeacherMapper teacherMapper;
	
	@Override
	public boolean onlyCheck(CreateTeacherDto teacher)
	{
		List<String> orgList = new ArrayList<String>();
		for(TeaCourseDto teaCourseDto : teacher.getTeaCourseList())
		{
			orgList.add(teaCourseDto.getOrgId().toString());
		}
		
		Map selectMap = new HashMap();
		selectMap.put("teacherName",teacher.getTeacherName());
		selectMap.put("orgList", orgList);
		if(0 != teacherMapper.onlyCheck(selectMap))
		{
			return true;
		}
		
		return false;
	}
	
	@Override
	public boolean onlyCheck(UpdateTeacherDto teacher)
	{
		List<String> orgList = new ArrayList<String>();
		for(TeaCourseDto teaCourseDto : teacher.getTeaCourseList())
		{
			orgList.add(teaCourseDto.getOrgId().toString());
		}
		
		Map selectMap = new HashMap();
		selectMap.put("teacherId", teacher.getTeacherId());
		selectMap.put("teacherName",teacher.getTeacherName());
		selectMap.put("orgList", orgList);
		if(0 != teacherMapper.onlyCheck(selectMap))
		{
			return true;
		}
		
		return false;
	}
	
	@Override
	public boolean createTeacher(CreateTeacherDto createTeacherDto) {
		// TODO Auto-generated method stub
		//取出基本信息
		Teacher teacher = new Teacher();
		teacher.setTeacherName(createTeacherDto.getTeacherName());
		teacher.setTeacherGender(createTeacherDto.getTeacherGender());
		teacher.setTeacherPhone(createTeacherDto.getTeacherPhone());
		boolean success = this.save(teacher);
		if(success) 
		{
			List<TeaCourseDto> teaCourseList = createTeacherDto.getTeaCourseList();
			List<OrgTea> orgTeaList = new ArrayList<OrgTea>();
			List<TeaCour> teaCourList = new ArrayList<TeaCour>();
			for (TeaCourseDto teaCourseDto : teaCourseList) {
				//取出所教授课程的集合
				//所在校区
				OrgTea orgTea = new OrgTea();
				orgTea.setTeacherId(teacher.getTeacherId());
				orgTea.setOrganizationId(teaCourseDto.getOrgId());
				orgTeaList.add(orgTea);
				//所在校区可教授的课程
				List<Long> courseIds = teaCourseDto.getCourseIdList();
				for (Long courseId : courseIds) {
					TeaCour teaCour = new TeaCour();
					teaCour.setCourseId(courseId);
					teaCour.setTeacherId(teacher.getTeacherId());
					teaCour.setOrgId(teaCourseDto.getOrgId());
					teaCourList.add(teaCour);
				}
			}
			orgTeaService.saveBatch(orgTeaList);
			teaCourService.saveBatch(teaCourList);
		}
		return success;
	}
	@Override
	public boolean updateTeacher(UpdateTeacherDto updateTeacherDto) {
		// TODO Auto-generated method stub
		//取出基本信息
		Teacher teacher = new Teacher();
		teacher.setTeacherId(updateTeacherDto.getTeacherId());
		teacher.setTeacherName(updateTeacherDto.getTeacherName());
		teacher.setTeacherGender(updateTeacherDto.getTeacherGender());
		teacher.setTeacherPhone(updateTeacherDto.getTeacherPhone());
		boolean success = this.updateById(teacher);
		if(success) 
		{
			
			//先删除再添加
			// 先删再加
			HashMap<String, Object> idMap = new HashMap<String, Object>();
			idMap.put("teacher_id", teacher.getTeacherId());
			log.info("开始删除老师所在校区");
			orgTeaService.removeByMap(idMap);
			log.info("结束删除老师所在校区");
			
			log.info("开始删除老师所教课程");
			teaCourService.removeByMap(idMap);
			log.info("结束删除老师所教课程");
			
			List<TeaCourseDto> teaCourseList = updateTeacherDto.getTeaCourseList();
			List<OrgTea> orgTeaList = new ArrayList<OrgTea>();
			List<TeaCour> teaCourList = new ArrayList<TeaCour>();
			for (TeaCourseDto teaCourseDto : teaCourseList) {
				//取出所教授课程的集合
				//所在校区
				OrgTea orgTea = new OrgTea();
				orgTea.setTeacherId(teacher.getTeacherId());
				orgTea.setOrganizationId(teaCourseDto.getOrgId());
				orgTeaList.add(orgTea);
				//所在校区可教授的课程
				List<Long> courseIds = teaCourseDto.getCourseIdList();
				for (Long courseId : courseIds) {
					TeaCour teaCour = new TeaCour();
					teaCour.setCourseId(courseId);
					teaCour.setOrgId(teaCourseDto.getOrgId());
					teaCour.setTeacherId(teacher.getTeacherId());
					teaCourList.add(teaCour);
				}
			}
			orgTeaService.saveBatch(orgTeaList);
			teaCourService.saveBatch(teaCourList);
		}
		return success;
	}
	
	@Override
	public Page<TeacherInfoVo> selectTeacherList(Page<TeacherInfoVo> page, QueryTeacherDto QueryTeacherDto) {
		Page<TeacherInfoVo> pageUserInfo = teacherMapper.selectTeacherList(page, QueryTeacherDto);
		return pageUserInfo;
	}
}
